package org.custom.spring.boot.mybatis.plugin.impl;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Signature;
import org.custom.spring.boot.mybatis.plugin.AbstractMybatisPlugin;
import org.custom.spring.boot.mybatis.service.plugin.algorithm.MybatisAlgorithm;
import org.custom.spring.boot.mybatis.service.plugin.algorithm.impl.MybatisExecuteLoggerAlgorithm;
import org.custom.spring.boot.mybatis.service.plugin.process.MybatisProcessor;
import org.custom.spring.boot.mybatis.service.plugin.process.impl.ExecuteLoggerMybatisProcessor;

import java.sql.Connection;

/**
 * Mybatis插件(打印SQL语句)
 * <p>
 * 注：重要，不支持非预编译SQL中 字段/数据/表名 携带 '\n' 或 '  ' 数据
 * 注：此处记录的SQL执行时间为 SQL查询 及 其余后续插件前置代码执行 所花费的时间
 *
 * @author 李坤
 */
@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}))
public class MybatisExecuteLoggerPlugIn extends AbstractMybatisPlugin {
    private final MybatisProcessor mybatisProcessor = new ExecuteLoggerMybatisProcessor();
    private final MybatisAlgorithm mybatisAlgorithm = new MybatisExecuteLoggerAlgorithm();

    @Override
    public MybatisProcessor obtainProcess() {
        return this.mybatisProcessor;
    }

    @Override
    public MybatisAlgorithm obtainMybatisAlgorithm() {
        return this.mybatisAlgorithm;
    }
}
